using System;
using Gtk;
using Gdk;
using Paret;
using System.Collections.Generic;
using System.IO;
using System.Drawing.Imaging;

public partial class ParetWindow: Gtk.Window
{	
	static string HOME = "HOME";
	static string START = "Start";
	static string STOP = "Stop";
	int gapNotification;
	ParetTimer timer;
	Papers papers;
	string paperFolder;
	
	public ParetWindow (): base (Gtk.WindowType.Toplevel)
	{

		Build ();
		//this.gapNotification = spintimer.ValueAsInt;
		//timer = new ParetTimer(this.gapNotification);
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}


	protected virtual void OnAboutActionActivated (object sender, System.EventArgs e)
	{
		PapersAboutDialog.ShowUp();
		
	}

	protected virtual void OnParetQuitActivated (object sender, System.EventArgs e)
	{
		this.Destroy();
	}

	protected virtual void OnAddButtonActivated (object sender, System.EventArgs e)
	{
		
		
		
	}

	protected virtual void OnAddButtonClicked (object sender, System.EventArgs e)
	{
		Console.WriteLine("hi");
		List<string> paretPapers = null;
		Gtk.FileChooserDialog picsFolder = new Gtk.FileChooserDialog("Choose folder with wallpapers",
		                                                     this,
		                                                     FileChooserAction.SelectFolder,
		                                                     "Cancel", ResponseType.Cancel,
		                                                     "Accept",ResponseType.Ok);
		
		picsFolder.SetCurrentFolder(Environment.GetEnvironmentVariable(HOME));
		if (picsFolder.Run() == (int)ResponseType.Ok){
			Console.WriteLine(picsFolder.Filename);
			papers = new Papers(picsFolder.Filename);
			paretPapers = papers.getPapers();
			
		}
		picsFolder.Destroy();
		if (paretPapers != null){
			foreach (string paper in paretPapers){
				//Console.WriteLine(paper);
				System.Drawing.Image sysimage = System.Drawing.Image.FromFile(paper);
				System.Drawing.Image thumbpaper = sysimage.GetThumbnailImage(128,128,new System.Drawing.Image.GetThumbnailImageAbort(thumbPaperAbortCallback),IntPtr.Zero);
				MemoryStream imageStream = new MemoryStream();

        // put the image into the memory stream
        		thumbpaper.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
				
//				byte[] imageContent =  new Byte[imageStream.Length];
//
//        // rewind the memory stream
       		imageStream.Position = 0;
//
//        // load the byte array with the image
//        	imageStream.Read(imageContent, 0, (int)imageStream.Length);

        // return byte array to caller with image type
//        	Response.ContentType = "image/jpeg";
//        	Response.BinaryWrite(imageContent);
				
				Gtk.Image image = new Gtk.Image(imageStream);
				image.Visible = true;
				//HBox paperBox = new HBox(true,6);
				//paperBox.Add(image);
				hbox2.PackStart(image,false,false,10);
			
				//break;
			}
		}
	}
	
	public bool thumbPaperAbortCallback()
	{
		return true;
	}
	
		
}